<?php

/**
 * For constructing unordered lists that will be used as a drop-down menu, with the right CSS.
 */
class MenuHelper extends AppHelper {
  
  var $helpers = array('Html');
  
  
  function __construct($options = null) {
  
    parent::__construct($options);
    
  } ///////////// end constructor /////////////////
  
  
  /**
   * makeListItem().  This constructs a <li> element with child <a> with optional child <img>
   *  and display text.  The <a> creation parts of this method are similar to the "Link" helper
   *  method "makeLink()". However, this method performs slightly different HTML construction,
   *  since it has to wrap certain children of the <li> in a span, etc to work properly with the
   *  CSS that I'm using. In constrast, "makeLink()" never performs such wrapping.  Thus, both methods
   *  have their place, and this method cannot use that method.
   *
   * @param string $linkToController
   * @param string $linkToMethod
   * @param array  $linkArgs        Arguments to pass to the linkToMethod
   * @param bool   $separateArgs    Whether or not to pass all the arg separately on the url line
   *                                ('true') or to just pass the array itself intact ('false').
   * @param bool   $isFirstTopMenuItem 
   * @param bool   $isLastTopMenuItem
   * @param string $linkDisplayStr  A string that you want displayed as the link text.
   * @param string $tooltip         Text to be displayed on hover on the item.
   * @param string $iconPath        Relative to 'webroot/img'
   * @param string $nestedList      A menu list to be 'under' this item. The list needs to be another
   *                                <ul> created the same way as this list is being created.
   * @param array  $linkOptions
   * @param array  $imgOptions
   * @param string $confirmMsg
   * @return string        The <li> with child <a> and display text, etc. 
   */
  function makeListItem($linkToController, $linkToMethod, $linkArgs = array(), $separateArgs = true,
                        $isFirstTopMenuItem = false, $isLastTopMenuItem = false,
                        $linkDisplayStr = null, $tooltip = null, 
                        $imagePath = null, $nestedList = null,
                        $linkOptions = array(), $imgOptions = array(), $confirmMsg = null) {
    // Set the image 'alt' attribute
    if($linkDisplayStr && !isset($imgOptions['alt'])) {
      $imgOptions['alt'] = $linkDisplayStr;
    }
    else if($tooltip && !isset($imgOptions['alt'])) {
      $imgOptions['alt'] = $tooltip;
    }
    // Create the <img> element if caller passed in a path
    $imageHtml = '';
    if($imagePath) {
      $imageHtml = $this->Html->image($imagePath, $imgOptions);   
    }
    // Set the link destination Controller/Action
    $linkToArray = null;
    // If we want to go somewhere, set things up
    if($linkToController != null && $linkToController != '') {
      $linkToArray = array('controller'=>$linkToController, 'action'=>$linkToMethod );
      // Add any arguments for the destination method to the URL array
      if($separateArgs) {
        foreach ($linkArgs as $arg ) {
          $linkToArray[] = $arg;
        }
      }
      else {
        $linkToArray[] = $linkArgs; // add entire array of args to the URL array
      }
    }
    // If we want to stay on same page, reconstruct the URL, with an appended '#'
    else if($linkToController==null || $linkToController== '') {
      //$linkToArray = array('#' => ''); // doesn't handle cases where current URL has args in it
      
      $linkToArray = array('controller'=>$this->params['controller'],
                           'action'    =>$this->params['action']);
      // add any original arguments back in to this reconstructed URL
      if(count($this->params['pass'])) {
        for($i = 0; $i < count($this->params['pass']); $i++) {
          if($i == (count($this->params['pass']) -1)) {
            $linkToArray[] = $this->params['pass'][$i] . "#"; // add # to last arg
          }
          else {
            $linkToArray[] = $this->params['pass'][$i];
          }
        }
      }
      // if no args, just add # to the array
      else {
        $linkToArray[] = "#";
      }
    } 
//    // Add any arguments for the destination method to the URL array
//    if($separateArgs) {
//      foreach ($linkArgs as $arg ) {
//        $linkToArray[] = $arg;
//      }
//    }
//    else {
//      $linkToArray[] = $linkArgs; // add entire array of args to the URL array
//    }
    
    // Set necessary link options
    $linkOptions['escape'] = false; 
    if( !isset($linkOptions['title'])) $linkOptions['title'] = $tooltip;
    
    // Set the display text string
    if(!$linkDisplayStr) {
      $linkDisplayStr = '';
    }
    // Concateneate the image icon html and the display string
    $entireLinkContents = "{$imageHtml}$linkDisplayStr";
    // Wrap a 'parent' anchor (has submenu elements) contents in a <span> element. Required for the menu css.
    if($nestedList) {
      $entireLinkContents = "<span>$entireLinkContents</span>";
    }
    // Create the <a> element
    $linkHtml = $this->Html->link($entireLinkContents, $linkToArray, $linkOptions, $confirmMsg);
        //debug($linkHtml);
    // ----------------------------------
    // prepare the contents of the <li> 
    $listItemContents = '';
    // If a nested list html str was passed in, need to make it a sibling to the link html
    if($nestedList) {
      $listItemContents = "$linkHtml\n$nestedList";
    }
    else {
      $listItemContents = $linkHtml;
    }
    
    // Create the <li>, with possible classes
    // First determine which classes should be applied
    $listItemHtml = '';
    $class = '';
    if($isFirstTopMenuItem) {
      $class = 'topfirst';
    }
    else if($isLastTopMenuItem) {
      $class = 'toplast';  
    }
    //  if this is a single-item list
    if($isFirstTopMenuItem && $isLastTopMenuItem) {
      $class = 'topFirstAndLast'; 
    }
    
    // Now apply the class
    if($class) {
      $listItemHtml = "<li class='$class'>$listItemContents</li>";
    }
    else {
      $listItemHtml = "<li>$listItemContents</li>";
    }
    return $listItemHtml;
    
  } ////////// end makeListItem() //////////////////

} //////////////////////// end class ////////////////////////////////

?>
